<template>
    <!-- 点击上传先发送游戏名和地址，获取到游戏id,然后上传文件 -->
    <el-dialog v-model="dialogVisible" title="创建游戏" width="800px" destroy-on-close center :show-close="false">
    <div class="mainContent">
        <div class="line">
            <span>游戏名称：</span>
            <el-input v-model="gameName"></el-input>
        </div>
        <div class="line">
            <span>游戏地址：</span>
            <el-input v-model="gameUrl"></el-input>
        </div>
        <div class="line">
            <span>游戏描述：</span>
            <el-input v-model="gameDescribe" type="textarea" :rows="4"></el-input>
        </div>
        <div class="line">
            <span>游戏图标：</span>
            <el-upload
                class="avatar-uploader"
                action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
            >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
        </div>
        <div class="line">
            <span>游戏图标：</span>
            <el-upload
                action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
            >
            <template #trigger>
                <el-button type="primary">select file</el-button>
            </template>
            </el-upload>
        </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancelClick">取消</el-button>
        <el-button type="primary" @click="sureClick">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { useLayoutStore } from '@/stores/layoutStore';
import { ref } from 'vue'
import type { UploadProps } from 'element-plus'
import { storeToRefs } from 'pinia'
// const props = defineProps(['dialogVisible'])
const imageUrl =ref('')
const store = useLayoutStore()
const gameName = ref('')
const gameUrl = ref('')
const gameDescribe = ref('')
const handleAvatarSuccess:UploadProps['onSuccess'] = (response,uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}
const dialogVisible = storeToRefs(store).createGamePopVisible


const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
    // ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    // ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  imageUrl.value = URL.createObjectURL(rawFile)
  return true
}

const cancelClick = ()=>{
    dialogVisible.value = false
}
const sureClick = ()=>{
    dialogVisible.value = false
}
</script>
<style>
.mainContent{

    display: flex;
    flex-direction: column;
    row-gap: 20px;
    .line{
        display: flex;
        align-items: center;

        span{
            width: 100px;
        }
    }
}
.avatar-uploader .avatar {
  width: 96px;
  height: 96px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 96px;
  height: 96px;
  text-align: center;
}
</style>